/*
 * Copyright 2008 the original author Ang Gao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * PostServiceImpl.java 2009-1-1
 */
package ie.ucc.cs.anggao.finalproject.server.service;

import ie.ucc.cs.anggao.finalproject.client.model.BlogServer;
import ie.ucc.cs.anggao.finalproject.client.service.PostService;
import ie.ucc.cs.anggao.finalproject.server.blogPost.GoogleBloggerClient;
import ie.ucc.cs.anggao.finalproject.server.blogPost.WordpressClient;

import java.util.HashMap;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * @author GaoAng
 * 
 */
public class PostServiceImpl extends RemoteServiceServlet implements
		PostService {

	public String PostEntity(BlogServer blog) {

		System.out.println(blog.getTitle());
		System.out.println(blog.isDraft());

		if (blog.getBlogtype().equals("Blogger")) {

			GoogleBloggerClient blogger = new GoogleBloggerClient(blog
					.getUsername(), blog.getPassword());
			try {
				blogger.newPost(blog.getTitle(), blog.getContent(), Boolean
						.valueOf(blog.isDraft()).booleanValue());
			} catch (Exception e) {
				e.printStackTrace();
				return "POST Failed!";
			}

		} else {

			WordpressClient wordpress = new WordpressClient();
			wordpress.setBlogurl(blog.getBlogaddress());
			wordpress.setUsername(blog.getUsername());
			wordpress.setPassword(blog.getPassword());

			HashMap hmContent = new HashMap();
			hmContent.put("title", blog.getTitle());
			hmContent.put("description", blog.getContent());
			hmContent.put("draft", Boolean.valueOf(blog.isDraft())
					.booleanValue());
			try {
				wordpress.post(hmContent);
			} catch (Exception e) {

				e.printStackTrace();
				return "POST Failed!";
			}
		}

		return "OK";

	}

}
